package models;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import libsvmCustom.svm_predict;

import org.apache.log4j.Logger;

import util.ResultTable;
import util.SystemUtil;
import feature.util.ListFromFile;
import frame.FeatureHandler;

/**
 * @author Rishi Das Roy
 *
 * @Organization Institute Of Genomics & Integrative Biology
 */
public class BaseModel implements Model {
	private static Logger log = Logger.getLogger(BaseModel.class);

	private Result mResult;

	private List<Float> mNoProbResult;

	/**
	 * generate required features scale features predict class with
	 * probabilities
	 *
	 * @param lfh
	 * @throws IOException
	 */
	public void predict(FeatureHandler lfh, File pWrkSpc) throws IOException {
		List<String> ftrList = ListFromFile.getNuLineList(new File(SystemUtil
				.getClassPath(mFeatureListPath)));
		if (mFeatureset.equals("Novel")) {
			for (int i = 0; i < ftrList.size(); i++) {
				int id = Integer.parseInt(ftrList.get(i));
				if (id > 3620) {
					id = (id - 3620) + 4856;
					ftrList.set(i, "" + id);
				}
			}
		}

		String test_file = lfh.extactScaledFeatures(this, ftrList);

		mOutput_file = pWrkSpc.getAbsolutePath() + File.separator + getName()
				+ File.separator + "Results.csv";
		String[] argv = new String[] { "-b", "1", test_file,
				SystemUtil.getClassPath(mModelPath), mOutput_file };
		svm_predict.main(argv);

		String no_prob = pWrkSpc.getAbsolutePath() + File.separator + getName()
				+ File.separator + "noProb_Results.csv";
		argv = new String[] { test_file, SystemUtil.getClassPath(mModelPath),
				no_prob };
		svm_predict.main(argv);

		String mOutput_file_id = pWrkSpc.getAbsolutePath() + File.separator
				+ getName() + File.separator + getName() + "_"
				+ "ResultsWithId.csv";
		addId(lfh, mOutput_file_id);
		// mResult = new Result(mOutput_file,lfh,this);
		mNoProbResult = readNoProb(no_prob);
		mResult = new Result(mNoProbResult, lfh, this);
		// mResult.writeResult(pWrkSpc.getAbsolutePath()+File.separator+getName()+"_performance.csv");

	}

	private List<Float> readNoProb(String no_prob) throws IOException {
		List<Float> lList = new ArrayList<Float>();
		BufferedReader lBr = new BufferedReader(new FileReader(no_prob));
		String line = null;
		while ((line = lBr.readLine()) != null) {
			lList.add(Float.parseFloat(line));
		}
		return lList;
	}

	private void addId(FeatureHandler lfh, String output_file_id)
			throws IOException {
		BufferedReader lBr = new BufferedReader(new FileReader(mOutput_file));
		BufferedWriter lBw = new BufferedWriter(new FileWriter(output_file_id));
		String line = lBr.readLine();
		lBw.write("Input Label," + line.replaceAll(" ", ",") + ",prot_id"
				+ System.getProperty("line.separator"));
		int i = 0;
		while ((line = lBr.readLine()) != null) {
			lBw.write(lfh.getActualLabels().get(i) + ","
					+ line.replaceAll(" ", ",") + lfh.getId().get(i++)
					+ System.getProperty("line.separator"));
		}
		lBw.close();
	}

	/*
	 * private void readResult() throws FileNotFoundException { BufferedReader
	 * lBR = new BufferedReader(new FileReader(mOutput_file)); String line;
	 * while((line=lBR.readLine())!=null){ String[] scores = line.split("\\s");
	 *
	 * for (int i = 1; i < scores.length; i++) { String id =
	 * scores[i].split(":")[0];
	 *  }
	 *  } }
	 */

	private String mName;

	private String mDescription;

	private String mFeatureset;

	private String mFeatureListPath;

	private String mScalePath;

	private String mModelPath;

	private String mOutput_file;

	private String mPosLabel = "1";

	private String mNegLabel = "-1";

	private float mBAC = 0;

	public String getDescription() {
		return mDescription;
	}

	public void setDescription(String description) {
		mDescription = description;
	}

	public String getFeatureListPath() {
		return mFeatureListPath;
	}

	public void setFeatureListPath(String featureListPath) {
		mFeatureListPath = featureListPath;
	}

	public String getFeatureset() {
		return mFeatureset;
	}

	public void setFeatureset(String featureset) {
		mFeatureset = featureset;
	}

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		mName = name;
	}

	public String getScalePath() {
		return mScalePath;
	}

	public void setScalePath(String scalePath) {
		mScalePath = scalePath;
	}

	public String getModelPath() {
		return mModelPath;
	}

	public void setModelPath(String modelPath) {
		mModelPath = modelPath;
	}

	public String getOutput_file() {
		return mOutput_file;
	}

	public void setOutput_file(String output_file) {
		mOutput_file = output_file;
	}

	public Result getResult() {
		return mResult;
	}

	public String getNegLabel() {
		return mNegLabel;
	}

	public void setNegLabel(String negLabel) {
		this.mNegLabel = negLabel;
	}

	public String getPosLabel() {
		return mPosLabel;
	}

	public void setPosLabel(String posLabel) {
		this.mPosLabel = posLabel;
	}

	public ResultTable getTable() {
		// TODO Auto-generated method stub
		return mResult.getMRT();
	}

	public float getBAC() {
		return mBAC;
	}

	public void setBAC(float mbac) {
		mBAC = mbac;
	}

	public List<Float> getNoProbResult() {
		return mNoProbResult;
	}

}
